var partNum;

$("#partNum").keyup(function(){
	creatTestInf();
});

$("#defineTest").click(function(){
	var testName=$("#testName").val();
	var totalPoint=$("#totalPoint").val();
	partNum=$("#partNum").val();
	var inputPoint=0;
	var allInput=true;
	
	for(var i=1;i<=partNum;i++){
		if($("#partName"+i).val()==""||$("#questionNum"+i).val()==0||$("#perQuestionPoint"+i).val()==0){
			allInput=false;
		}
		inputPoint+=($("#questionNum"+i).val())*($("#perQuestionPoint"+i).val());
	}
	if(!allInput){
		alert("信息未输入完整。");
	}else if(inputPoint>totalPoint&&allInput){
		alert("你的实际总分值大于预设总分值，请重新编辑题目分值。");
	}else if(inputPoint<totalPoint&&allInput){
		alert("你的实际总分值小于预设总分值，请重新编辑题目分值。");
	}else if(inputPoint=totalPoint&&allInput){
		$.ajax({
			type:"post",
			url:"../createTest",
			data:{
				testName:testName,
				totalPoint:totalPoint,
				partNum:partNum,
				type:0,
			},
			success:function(data){
				$("#testName").attr("disabled","disabled");
				$("#totalPoint").attr("disabled","disabled");
				$("#partNum").attr("disabled","disabled");
				for(var i=1;i<=partNum;i++){
					$("#partName"+i).attr("disabled","disabled");
					$("#typeList"+i).attr("disabled","disabled");
					$("#questionNum"+i).attr("disabled","disabled");
					$("#perQuestionPoint"+i).attr("disabled","disabled");
						
					createPart($("#partName"+i).val(),i,$("#questionNum"+i).val(),$("#perQuestionPoint"+i).val(),$("#typeList"+i).val());
					
					addTopic(i,$("#questionNum"+i).val());
					
				}
			}
		});  
		
		$("#defineTest").remove();
		//$("#preserveButton").append("<div class=\"form-group mx-sm-3 mb-2\" align=\"center\"><button class=\"btn btn-outline-success btn-lg\" id=\"preserve\">保存</button></div>");
		$("#preserve").removeAttr("disabled");
	}
	
});

function createPart(partName,partNo,questionNum,perQuestionPoint,QuestionType){
	$.ajax({
		url:"../createTest",
		type:"post",
		data:{
			partName:partName,
			partNo:partNo,
			questionNum:questionNum,
			perQuestionPoint:perQuestionPoint,
			QuestionType:QuestionType,
			type:1
		},
		async: false,
		dataType: "html",
		success:function(res){
		
		}
	});	
}
/*
 * 前端已配置好，现在需要建数据库part和写java插入。
 * */

function creatTestInf(){
	partNum=$("#partNum").val();
	$("#parts").empty();
	for(var i=1;i<=partNum;i++){
		$.ajax({
			url:"htmlPart/parts.html",
			type:"post",
			data:{},
			async: false,
			dataType: "html",
			success:function(res){
				$("#parts").append($(res));
				
				$("#partNo").html("第"+i+"部分");
				$("#partNo").attr("id","partNo"+i);
				$("#partNo"+i).attr("name",i);
				
				$("#partName").attr("id","partName"+i);
				$("#typeList").attr("id","typeList"+i);
				$("#questionNum").attr("id","questionNum"+i);
				$("#perQuestionPoint").attr("id","perQuestionPoint"+i);
				
				$("#questions").attr("name",i);
				$("#questions").attr("id","questions"+i);			
				
			}
		});	
	}
};
/*
 * for为单线程，ajax为多线程，循环调用ajax时会出现异步，只需要把ajax改成同步的就可以了，每次for循环，
 * 都要去加载ajax方法，并且拿到他返回的数据，只需要在ajax中间加一个代码：async: false。
*/


/*
 * 添加三种题目
 * */
function addTopic(partNo,questionNum){
	if($("#typeList"+partNo).val()==1){
		for(var i=1;i<=questionNum;i++){
			addSingle(partNo,i);
			addQuestion(partNo,i,1);
		}	
	}else if($("#typeList"+partNo).val()==2){
		for(var i=1;i<=questionNum;i++){
			addMultiple(partNo,i);
			addQuestion(partNo,i,2);
		}
	}else if($("#typeList"+partNo).val()==3){
		for(var i=1;i<=questionNum;i++){
			addSubject(partNo,i);
			addQuestion(partNo,i,3);
		}
	}
	
}

function addSingle(partNo,questionNo){
	$.ajax({
		url:"htmlPart/single.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 单选";
			
			$("#new").addClass("1");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。搜索、取消、保存。
			
			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			
			$("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");
			
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
			
			
		}
	});		
}

function addMultiple(partNo,questionNo){
	$.ajax({
		url:"htmlPart/multiple.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 多选";
			
			$("#new").addClass("2");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。
			
            $("#question").attr("id",partNo+"topic"+questionNo+"q");
			
            $("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");
			
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
		
		}
	});		
};
function addSubject(partNo,questionNo){
	$.ajax({
		url:"htmlPart/subject.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 主观题";
			
			$("#new").addClass("3");
			$("#new").attr("id",partNo+"topic"+questionNo);
			
			setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。
			
			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			$("#answer").attr("id",partNo+"topic"+questionNo+"a");
			
			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
		}
	});		
}

function addQuestion(partNo,questionNo,questionType){
	$.ajax({
		url:"../createTest",
		type:"post",
		data:{
			partNo:partNo,
			questionNo:questionNo,
			questionType:questionType,
			type:2,
		},
		async: false,
		dataType: "html",
		success:function(res){		
			
		}
	});	
}

function setSearchCard(partNo,questionNo,type){
	$("#searchQuestion").attr("id",partNo+"topic"+questionNo+"searchQuestion");
	$("#"+partNo+"topic"+questionNo+"searchQuestion").click(function(){addSearchResult(partNo,questionNo);});//搜索 按钮被点击时
	
	$("#cancel").attr("id",partNo+"topic"+questionNo+"cancel");
	$("#"+partNo+"topic"+questionNo+"cancel").click(function(){cancelSearch(partNo,questionNo);});//取消按钮被点击时
	
	$("#preserve").attr("id",partNo+"topic"+questionNo+"preserve");
	$("#"+partNo+"topic"+questionNo+"preserve").click(function(){preserve(partNo,questionNo,type);});//保存 按钮被点击时
	
	$("#search").attr("id",partNo+"topic"+questionNo+"search");//搜索卡片的区域
}

function addSearchResult(partNo,questionNo){
	$("#"+partNo+"topic"+questionNo+"cancel").removeAttr("disabled");
	$("#"+partNo+"topic"+questionNo+"searchQuestion").attr("disabled","disabled");
	$.ajax({
		url:"htmlPart/search.html",
		type:"post",
		data:{},
		async: false,
		dataType: "html",
		success:function(res){		
			$("#"+partNo+"topic"+questionNo+"search").append(res)
			
			$("#keyWord").attr("id",partNo+"topic"+questionNo+"keyWord");
			$("#subject").attr("id",partNo+"topic"+questionNo+"subject");
			$("#knowledgePoint").attr("id",partNo+"topic"+questionNo+"knowledgePoint");
			$("#keyWordSearchContent").attr("id",partNo+"topic"+questionNo+"keyWordSearchContent");
			$("#classSearchContent").attr("id",partNo+"topic"+questionNo+"classSearchContent");
		}
	});
	//在此处绑定输入检索信息时的操作。
	$("#"+partNo+"topic"+questionNo+"keyWord").keyup(function(){keyWordSearch(partNo,questionNo,$("#typeList"+partNo).val());});
	$("#"+partNo+"topic"+questionNo+"subject").change(function(){classSearch(partNo,questionNo,$("#typeList"+partNo).val())});
	$("#"+partNo+"topic"+questionNo+"knowledgePoint").keyup(function(){classSearch(partNo,questionNo,$("#typeList"+partNo).val())});
}

function keyWordSearch(partNo,questionNo,type){
	$("#"+partNo+"topic"+questionNo+"keyWordSearchContent").empty();
	var keyWord=$("#"+partNo+"topic"+questionNo+"keyWord").val();
	if(keyWord!=""){
		$.ajax({
			url:"../keyWordSearch",
			type:"post",
			data:{
				keyWord:keyWord,
				type:type,
			},
			async: false,
			success:function(res){		
				var num = res.count;
				for(var i=1;i<=num;i++){
					loadSearchResult(partNo,questionNo,i,type,res["questionId"+i],res["topic"+i],"keyWordSearchContent");
				}
			}
		});	
	}
}

function classSearch(partNo,questionNo,type){
	$("#"+partNo+"topic"+questionNo+"classSearchContent").empty();
	var subject=$("#"+partNo+"topic"+questionNo+"subject").val();
	var knowledgePoint=$("#"+partNo+"topic"+questionNo+"knowledgePoint").val();

	$.ajax({
		url:"../classSearch",
		type:"post",
		data:{
			subject:subject,
			knowledgePoint:knowledgePoint,
			type:type,
		},
		async: false,
		success:function(res){		
			var num = res.count;
			for(var i=1;i<=num;i++){
				loadSearchResult(partNo,questionNo,i,type,res["questionId"+i],res["topic"+i],"classSearchContent");
			}
		}
	});	

}

function loadSearchResult(partNo,questionNo,no,type,questionId,topic,content){
	$("#"+partNo+"topic"+questionNo+content).append("<button type=\"button\" class=\"btn btn-outline-secondary btn-small btn-block\" id=\""+partNo+"topic"+questionNo+content+no+"\">"+topic+"</button>");
	$("#"+partNo+"topic"+questionNo+content+no).click(function(){
		//console.log(topic);
		/*
		 * 在这里绑定搜索结果点击时的效果。
		 * */
		$.ajax({
			url:"../loadQuestionInf",
			type:"post",
			data:{
				questionId:questionId,
				type:type,
			},
			async: false,
			success:function(res){		
				if(type==1){
					$("#"+partNo+"topic"+questionNo+"q").val(topic);
					$("#"+partNo+"topic"+questionNo+"A").val(res.A);
					$("#"+partNo+"topic"+questionNo+"B").val(res.B);
					$("#"+partNo+"topic"+questionNo+"C").val(res.C);
					$("#"+partNo+"topic"+questionNo+"D").val(res.D);
					
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='"+res.answer+"']").attr("checked",true);
					
					$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}else if(type==2){
					$("#"+partNo+"topic"+questionNo+"q").val(topic);
					$("#"+partNo+"topic"+questionNo+"A").val(res.A);
					$("#"+partNo+"topic"+questionNo+"B").val(res.B);
					$("#"+partNo+"topic"+questionNo+"C").val(res.C);
					$("#"+partNo+"topic"+questionNo+"D").val(res.D);
					
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",false);
					$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",false);
	
					if(res.answer_A!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",true);}
					if(res.answer_B!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",true);}
					if(res.answer_C!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",true);}
					if(res.answer_D!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",true);}
					
					$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}else if(type==3){
					$("#"+partNo+"topic"+questionNo+"q").val(topic);
					$("#"+partNo+"topic"+questionNo+"a").val(res.answer);
					
                    $("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}
			}
		});	
		
	});
}

function cancelSearch(partNo,questionNo){
	$("#"+partNo+"topic"+questionNo+"cancel").attr("disabled","disabled");
	$("#"+partNo+"topic"+questionNo+"searchQuestion").removeAttr("disabled");
	$("#"+partNo+"topic"+questionNo+"search").empty();
}
function preserve(partNo,questionNo,type){
	if(type==1){
		$.ajax({
			url:"../createSingle",
			type:"post",
			data:{
				partNo:partNo,
				questionNo:questionNo,
				topic:$("#"+partNo+"topic"+questionNo+"q").val(),
				A:$("#"+partNo+"topic"+questionNo+"A").val(),
				B:$("#"+partNo+"topic"+questionNo+"B").val(),
				C:$("#"+partNo+"topic"+questionNo+"C").val(),
				D:$("#"+partNo+"topic"+questionNo+"D").val(),
				answer:GetRadioValue(partNo+"topic"+questionNo+"answer"),
				setSubject:$("#"+partNo+"topic"+questionNo+"setSubject").val(),
				setKnowledgePoint:$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(),
			},
			async: false,
			success:function(res){		
				if(res.code!=0){
					alert("保存成功");
				}else alert("保存失败");
			}
		});	
	}else if(type==2){
		$.ajax({
			url:"../createMultiple",
			type:"post",
			data:{
				partNo:partNo,
				questionNo:questionNo,
				topic:$("#"+partNo+"topic"+questionNo+"q").val(),
				A:$("#"+partNo+"topic"+questionNo+"A").val(),
				B:$("#"+partNo+"topic"+questionNo+"B").val(),
				C:$("#"+partNo+"topic"+questionNo+"C").val(),
				D:$("#"+partNo+"topic"+questionNo+"D").val(),
				a_A:GetCheckboxValue(partNo+"topic"+questionNo+"answer")[0],
				a_B:GetCheckboxValue(partNo+"topic"+questionNo+"answer")[1],
				a_C:GetCheckboxValue(partNo+"topic"+questionNo+"answer")[2],
				a_D:GetCheckboxValue(partNo+"topic"+questionNo+"answer")[3],
				setSubject:$("#"+partNo+"topic"+questionNo+"setSubject").val(),
				setKnowledgePoint:$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(),
			},
			async: false,
			success:function(res){		
				if(res.code!=0){
					alert("保存成功");
				}else alert("保存失败");
			}
		});	
	}else if(type==3){
		$.ajax({
			url:"../createSubject",
			type:"post",
			data:{
				partNo:partNo,
				questionNo:questionNo,
				topic:$("#"+partNo+"topic"+questionNo+"q").val(),
				answer:$("#"+partNo+"topic"+questionNo+"a").val(),
				setSubject:$("#"+partNo+"topic"+questionNo+"setSubject").val(),
				setKnowledgePoint:$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(),
			},
			async: false,
			success:function(res){		
				if(res.code!=0){
					alert("保存成功");
				}else alert("保存失败");
			}
		});	
	}
}

function GetRadioValue(RadioName){
    var obj;   
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
                return obj[i].value;           
            }
        }
    }else return null;
}

function GetCheckboxValue(RadioName){
    var obj;   
    var val=new Array();
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
            	val[i]=obj[i].value;        
            }
        }
        return val;
    }else return null;
}

$("#preserve").click(function(){
	var preserve=true;
	partNum=$("#partNum").val();
	for(var i=1;i<=partNum;i++){
		var questionNum=$("#questionNum"+i).val();
		for(var x=1;x<=questionNum;x++){
			$.ajax({
				url:"../finishCheck",
				type:"post",
				data:{
					partNo:i,
					questionNo:x,
					type:1,
				},
				async: false,
				success:function(res){
					if(res.code==0){
						preserve=false;
					}
				}
			});	
		}
	}
	
	if(preserve){
		$.ajax({
			url:"../finishCheck",
			type:"post",
			data:{
				partNo:0,
				questionNo:0,
				type:2,
			},
			async: false,
			success:function(res){
				if(res.code==1){
					alert("保存成功");
					$(window).attr('location','checkItem.jsp');
				}else alert("保存失败");
			}
		});	
	}else alert("你还有未编辑完的题目！");
});